const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 是否为修改
    id: "",
    // 银行卡号
    bankCardNumber: "",
    // 持卡人姓名
    realName: "",
    // 身份证号
    idCard: "",
    bankName: "",
    active: "",
    bankcardList: [],
    // 结账需要使用的参数
    unsettled: {}
  },

  getData() {
    wx.showLoading({
      title: "获取中..."
    });
    wx.$apis
      .getBankcardMarketPage({
        asc: true,
        columns: [],
        current: 1,
        size: 1000,
        companyId: app.globalData.userInfo.companyData.companyId
      })
      .then((res) => {
        if (res.data) {
          this.setData({
            bankcardList: res.data.records
          });
        }
      });
  },

  onChange(e) {
    let active = e.currentTarget.dataset.index;
    if (this.data.active === active * 1) {
      this.setData({
        active: ""
      });
      return;
    }
    this.setData({
      active,
      bankName: this.data.bankcardList[active].bankName,
      bankCardNumber: this.data.bankcardList[active].bankCardNumber
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.getStorage({
      key: "unsettled",
      success: (res) => {
        const unsettled = res.data.unsettled;
        this.setData({ unsettled });
        wx.removeStorage({
          key: "unsettled"
        });
      }
    });
  },

  onShow() {
    this.getData();
  },

  toAddBankcard() {
    if (this.data.active === "") {
      return wx.showToast({
        title: "请选择一张银行卡",
        icon: "none"
      });
    }
    const { unsettled, bankCardNumber, bankName, id } = this.data;
    wx.setStorage({
      key: "unsettled",
      data: {
        unsettled: { ...unsettled, bankCardNumber, bankName, id }
      },
      success: function () {
        wx.navigateBack(); //返回上一个页面
      }
    });
  }
});
